// 防抖
function debounce(fn, delay) {
  let timer = null;
  return function () {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      fn.call(this, arguments)
    }, dalay)
  }
}

// 节流~~~
function throttle(fn, delay) {
  let valid = true;
  return function () {
    if (!valid) {
      return
    }
    valid = false;
    setTimeout(() => {
      fn.call(this, arguments);
      valid = true;
    })
  }
}
// 进阶版节流
function throttle(fn, delay) {
  let timer = null;
  let startTime = Date.now()

  return function () {
    let nowTime = Date.now();
    let remainTime = delay - (nowTime - startTime)
    clearTimeout(timer)
    if (remainTime <= 0) {
      fn.call(this, arguments);
      startTime = nowTime
    } else {
      setTimeout(() => {
        fn.call(this, arguments);
      }, delay)
    }
  }
}